package com.kly.user.task;

import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.api.Response;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.service.MemberService;
import com.kly.user.db.dao.*;
import com.kly.user.db.entity.UserConversationChatterEntity;
import com.kly.user.db.entity.UserConversationEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.enums.LikeEnum;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ConversationRemoveTask {

    private static Log logger = LogFactory.getLog(ConversationRemoveTask.class);
    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    @NacosValue(value = "${chat.conversation.flag:1}", autoRefreshed = true)
    private Integer chatConversationFlag;

    private String localIP = NetUtil.getLocalhostStr();

    @Resource
    private UserConversationDao userConversationDao;

    @Resource
    private UserConversationChatterDao userConversationChatterDao;

    @Resource
    private MemberService memberService;

    @Resource
    private UserDao userDao;

    @Resource
    private UserMsgUnreadNumDao userMsgUnreadNumDao;

    @Resource
    private UserWlmDao userWlmDao;

    /**
     * 线上未启用
     */
//    @Scheduled(fixedDelay = 60000 * 60)
//    @Scheduled(fixedDelay = 500)
    public void execute() {

        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        if (chatConversationFlag == 1) {
            return;
        }

        Calendar memberTime = Calendar.getInstance();//获取对日期操作的类对象
        memberTime.add(Calendar.DATE, -7);
//        memberTime.add(Calendar.MINUTE, -60);
        //读取建联数据
        List<UserConversationChatterEntity> userConversationEntityList = userConversationChatterDao.getUserLastConversationList(memberTime.getTime());
        //对应数据
        for (UserConversationChatterEntity conversation : userConversationEntityList) {
            clearData(conversation);
        }

        Calendar noMemberTime = Calendar.getInstance();//获取对日期操作的类对象
        noMemberTime.add(Calendar.DATE, -3);
//        noMemberTime.add(Calendar.MINUTE, -30);
        List<UserConversationChatterEntity> userConversationEntities = userConversationChatterDao.getUserLastConversationList(noMemberTime.getTime());
        for (UserConversationChatterEntity conversation : userConversationEntities) {
            UserEntity userEntity = userDao.selectByUserCode(conversation.getUserCode());
            if (userEntity != null) {
                Response<Boolean> booleanResponse;
                if (userEntity.getUserType() == 4) {
                    booleanResponse = memberService.ifMember(conversation.getUserCode());
                } else {
                    booleanResponse = memberService.ifMember(conversation.getUserCodeFriend());
                }
                if (booleanResponse != null && booleanResponse.isSuccess()) {
                    if (!booleanResponse.getData()) {
                        clearData(conversation);
                    }
                }
            }
        }
    }

    /**
     * 数据清除
     *
     * @param conversation
     */
    private void clearData(UserConversationChatterEntity conversation) {
        userConversationChatterDao.doDeleted(conversation.getUserCode(), conversation.getUserCodeFriend());
//        userMsgUnreadNumDao.readMessage(conversation.getUserCode(), conversation.getUserCodeFriend());
        userWlmDao.removeOverEffectiveRecord(conversation.getUserCode(), conversation.getUserCodeFriend(), LikeEnum.LIKE_EACH_OTHER);
        userConversationChatterDao.doDeleted(conversation.getUserCodeFriend(), conversation.getUserCode());
//        userMsgUnreadNumDao.readMessage(conversation.getUserCodeFriend(), conversation.getUserCode());
        userWlmDao.removeOverEffectiveRecord(conversation.getUserCodeFriend(), conversation.getUserCode(), LikeEnum.LIKE_EACH_OTHER);
    }

}
